<template>
  <div style="margin-top: 80px">
    <div class="big" >账户注册</div>
    <el-divider></el-divider>
    <div class="small">请填写账户信息</div>
    </div>
  <div style="margin-top: 50px">
    <el-form :model="form" :rules="rules" @validate="onValidate" ref="formRef">
      <el-form-item prop="username">
        <el-input v-model="form.username" type="text" placeholder="用户名">
          <template #prefix>
            <el-icon ><User /></el-icon>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <div>
          <el-input style="width: 400px" v-model="form.password"  type="password" placeholder="密码">
            <template #prefix>
              <el-icon ><Lock /></el-icon>
            </template>
          </el-input>
        </div>
      </el-form-item>
      <el-form-item prop="password_repeat">
        <div >
          <el-input style="width: 400px" v-model="form.password_repeat"  type="password" placeholder="确认密码">
            <template #prefix>
              <el-icon ><Lock /></el-icon>
            </template>
          </el-input>
        </div>
      </el-form-item>
      <el-form-item prop="email">
        <div >
          <el-input style="width: 400px" v-model="form.email"  type="text" placeholder="邮箱">
            <template #prefix>
              <el-icon ><Message /></el-icon>
            </template>
          </el-input>
        </div>
      </el-form-item>
      <el-form-item prop="code">
        <div>
          <el-row :gutter="10" >
            <el-col :span="17">
              <el-input style="width: 290px" v-model="form.code" type="email" placeholder="请输入邮箱验证码">
                <template #prefix>
                  <el-icon><Bell /></el-icon>
                </template>
              </el-input>
            </el-col>
            <el-col :span="6">
              <el-button @click="validateEmail" type="primary" style="margin-left: 7px" :disabled="!isEmailValid">获取验证码</el-button>
            </el-col>
          </el-row>
        </div>
      </el-form-item>
    </el-form>





  </div>
  <div style="margin-top: 50px">
    <el-button @click="router.push('/')" type="primary" style="margin-right: 100px" >返回登录</el-button>
    <el-button type="success" @click="register" >立即注册</el-button>
  </div>
</template>
<script setup>

import {Message} from "@element-plus/icons-vue";
import router from "@/router";
import {reactive, ref} from "vue";
import {isValid} from "js-base64";
import {ElMessage} from "element-plus";
import {post} from "@/net";

const form = reactive({
  username:'',
  password:'',
  password_repeat:'',
  email:'',
  code:''
});

const validateUsername = (rule,value,callback) => {
  if (value === '') {
    callback(new Error('请输入用户名'))
  }
  else if(!/^[a-zA-Z0-9\u4e00-\u9fa5]+$/.test(value)){
    callback(new Error('用户名不能包含特殊字符'))
  }else {
    callback()
  }
};
const validatePassword1 = (rule,value,callback) => {
  if (value === '') {
    callback(new Error('请输入密码'))
  }
  else if(/^[\u4e00-\u9fa5]+$/.test(value)){
    callback(new Error('密码不能包含中文'))
  }else {
    form.password=value;
    callback()
  }
};
const validatePassword2 = (rule,value,callback) => {
  if (value === '') {
    callback(new Error('请确认密码'))
  }
  else if(value!=form.password){
    callback(new Error('两次密码输入结果不同，请重新确认'))
  }else {
    callback()
  }
};

const rules = {
  username: [
    {validator :validateUsername,trigger:'blur'},
    {min:2,max:8,message: '用户名长度为2~8个字符',trigger:['blur','change']}
  ],
  password:[
    {validator:validatePassword1,trigger:'blur'},
    {min:6,max:12,message: '密码长度为6~12个字符',trigger:['blur','change']}
  ],
  password_repeat:[
    {validator:validatePassword2,trigger:'blur'},
    {min:6,max:12,message: '密码长度为6~12个字符',trigger:['blur','change']}
  ],
  email:[
    {required: true, message: '请输入邮箱地址', trigger: 'blur'},
    {type: 'email', message: '请输入合法的邮件地址', trigger: ['blur', 'change']},
  ],
  code:[
    {required: true, message: '请输入验证码', trigger: 'blur'},
    {min:4,max:18 ,message: '请输入正确的验证码', trigger: ['blur', 'change']},
  ]
};


const isEmailValid = ref( false)
const formRef=ref()


const  onValidate = (prop,isValid) =>{
  if(prop ==='email')
    isEmailValid.value=isValid;

};

const register=()=>{
  formRef.value.validate((isValid) => {
    if(isValid){
      post('user/add',{
        username:form.username,
        password:form.password
      },(message)=>{
        ElMessage.success(message)
      })
    }else {
      ElMessage.warning('请完整填写注册表单内容')
    }
  })
};
const validateEmail = () => {
  post('api/auth/valid-email',{
    email: form.email
  },(message)=>{
    ElMessage.success(message)
  })
};

</script>
<style scoped>
</style>
